<html>

<style>
    * {
        margin: 0;
    }

    #content {
        color: white;
        position: absolute;
        left: -20px;
        top: -10px;
        white-space: pre-wrap;
        height: 200px;
        overflow: auto;
    }

</style>

<script type="importmap">
	{
	  "imports": {
		"three": "../../three.js-r151/build/three.module.js",
		"three/addons/": "../../three.js-r151/examples/jsm/"
	  }
	}
</script>

<body>
    <div id="content">
        不用html，单纯sprite也可以作为标签
        只是不灵活，得运用图片作为texture
    </div>
    <div id="canvas"></div>

    <script type="module">
        import * as THREE from 'three';
        import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
        // 引入CSS2渲染器CSS2DRenderer和CSS2模型对象CSS2DObject
        import { CSS2DRenderer, CSS2DObject } from 'three/addons/renderers/CSS2DRenderer.js';
        // 引入CSS3渲染器CSS3DRenderer
        import { CSS3DRenderer, CSS3DObject } from 'three/addons/renderers/CSS3DRenderer.js';
        // 引入CSS3精灵模型对象CSS3DSprite
        import { CSS3DSprite } from 'three/addons/renderers/CSS3DRenderer.js';


        // 1. 必须创建的三大件
        // 1.1 三维场景Scene, 类似创建了个世界，三维世界
        const scene = new THREE.Scene();
        const geometry = new THREE.BoxGeometry(1, 1, 1);
        const material = new THREE.MeshBasicMaterial({ color: 'yellow' });
        const cube = new THREE.Mesh(geometry, material);
        scene.add(cube);

        // 添加辅助坐标系
        const axesHelper = new THREE.AxesHelper(5);
        scene.add(axesHelper);

        // 1.2 相机
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.set(3, 3, 3)

        // 1.3 渲染器
        const renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        let canvasDom = document.getElementById("canvas");
        canvasDom.appendChild(renderer.domElement);

        new OrbitControls(camera, canvasDom);

        // 3. 不用html，单纯sprite也可以作为标签
        const texture = new THREE.TextureLoader().load("./warning.png");
        const spriteMaterial = new THREE.SpriteMaterial({
            map: texture,
            side: THREE.DoubleSide,
        });
        const sprite = new THREE.Sprite(spriteMaterial);
        scene.add(sprite)
        sprite.position.set(0, 1.8, 0)
        sprite.scale.set(.8, 1.6, 0)
        cube.scale.set(2, 2, 2)


        function render() {
            renderer.render(scene, camera);
            requestAnimationFrame(render)
        }
        render();

        // 画布跟随窗口变化
        window.onresize = function () {
            const width = window.innerWidth;
            const height = window.innerHeight;
            // cnavas画布宽高度重新设置
            renderer.setSize(width, height);
            camera.aspect = width / height;
            camera.updateProjectionMatrix();
        };


    </script>
</body>

</html>